package com.ckm.controller;

import com.alibaba.fastjson2.JSONArray;
import com.ckm.annotation.LogInfo;
import com.ckm.entity.StudentCourse;
import com.ckm.entity.ResponseResult;
import com.ckm.service.StudentCourseService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@LogInfo
@Validated
@RestController
@RequestMapping("studentCourse")
public class StudentCourseController {

    @Autowired
    private StudentCourseService studentCourseService;

    /**
     * 添加选课
     * @param studentCourseList 选课信息list
     * @return 成功信息
     */
    @PostMapping("addStudentCourse")
    public ResponseResult addStudentCourse(@RequestBody @Valid List<StudentCourse> studentCourseList) {
        studentCourseService.addStudentCourse(studentCourseList);
        return new ResponseResult(200,"添加成功",null);
    }

    /**
     * 更新选课
     * @param studentCourse 更新的信息
     * @return 成功信息
     */
    @PutMapping
    public ResponseResult updateStudentCourse(@RequestBody StudentCourse studentCourse) {
        studentCourseService.updateStudentCourse(studentCourse);
        return new ResponseResult(200,"更新成功","");
    }

    /**
     * 删除选课
     * @param id 选课Id
     * @return 成功信息
     */
    @DeleteMapping
    public ResponseResult deleteStudentCourse(@RequestParam(name = "id") Integer id) {
        studentCourseService.deleteStudentCourse(id);
        return new ResponseResult(200,"删除成功","");
    }

    /**
     * 查找选课信息
     * @param studentCourse 查找词
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 分页结果集
     */
    @PostMapping("find")
    public ResponseResult findStudentCourse(@RequestBody StudentCourse studentCourse, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200,"查找完毕",studentCourseService.getStudentCoursePage(studentCourse,pageNum,pageSize));
    }

    /**
     * 统计课程学生分数，分为及格与不及格，并展示通过率
     * @param courseName 课程名称
     * @return 统计结果
     */
    @GetMapping("statistics")
    public ResponseResult statistic(String courseName){
        JSONArray jsonArray = new JSONArray(studentCourseService.statistics(courseName));
        log.info("统计结果:{}",jsonArray);
        return new ResponseResult(200,"统计结果",jsonArray);
    }

    /**
     * 查看某专业下，所有学生成绩
     * @param majorName 专业名称
     * @return 统计后的JSONArray
     */
    @GetMapping("allScore")
    public ResponseResult allStudentCourseScore(@RequestParam(name = "majorName") String majorName, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return new ResponseResult(200,majorName+"专业下各学生课程分数",
                studentCourseService.allStudentCourseByMajor(pageNum,pageSize,majorName));
    }

    /**
     * 导出excel文件
     * @param id 教师id
     * @param courseName 课程名称
     */
    @GetMapping("out")
    public void getStudentCourse(@RequestParam(name = "tchId") Integer id,
                                           @RequestParam(name = "courseName") String courseName,
                                           HttpServletResponse response) {
        studentCourseService.outputStudentScore(courseName,id,response);
    }
}
